﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library.HTTP
{
    public class SpiderClientHepler
    {
        #region Get
        /// <summary>
        /// 获取到HTML页面
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string Get(string url, int timeout = 1000, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.GetEncoding("GB2312");
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = timeout;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader stream = new StreamReader(response.GetResponseStream(), encoding);
            string responseBody = stream.ReadToEnd();
            stream.Close();
            response.Close();
            return responseBody;
        }
        #endregion

        static HttpClientChrome _httpClientChrome;

        #region ChromeGet
        /// <summary>
        /// 使用 Chrome 模拟 Get 请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ChromeGet(string url, string cookie = "")
        {
            if (_httpClientChrome == null)
            {
                _httpClientChrome = new HttpClientChrome();
            }
            var task = _httpClientChrome.GetResponse("", url, cookie: cookie);
            return task.Result;
        }
        #endregion

        HttpClientChrome _httpClientChromes;

        #region 多线程抓取 --每一个线程都能创建一个实例  去抓取数据
        //到公司之后再弄这个东西

        #endregion

    }
}
